package com.lzx.iteam.net;

import android.os.Message;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liukun on 15-2-1.
 */
public class AddContactListMsg extends CcMsgStructure {
    private Message mmCallback;
    private int mTotalSize;

    public AddContactListMsg(Message callback) {
        mmCallback = callback;
    }
    public AddContactListMsg(Message callback,int totalSize) {
        mmCallback = callback;
        mTotalSize=totalSize;
    }


    @Override
    public void onReceive(JSONObject data) {
        try {
            JSONObject jsonObject = data.getJSONObject("data");
            Map<String, Object> map = new HashMap<String, Object>();
            if(jsonObject.has("contact_id")){
                map.put("contact_id",jsonObject.getString("contact_id"));
            }
            if(jsonObject.has("err_num")){
                map.put("err_num", jsonObject.getString("err_num"));
            }
            if(jsonObject.has("contacts_list_num")){
                map.put("contacts_list_num", jsonObject.getString("contacts_list_num"));
            }

            if(jsonObject.has("success_num")){
                map.put("success_num", jsonObject.getString("success_num"));
            }

            if(jsonObject.has("err_contact_names")){
                map.put("err_contact_names", jsonObject.getString("err_contact_names"));
            }

            if (mmCallback != null) {
                mmCallback.arg1=0;
                mmCallback.obj = map;
                mmCallback.arg2=mTotalSize;
                mmCallback.sendToTarget();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }

    }
}
